require.config({
	paths : {
		"jquery" : "jquery",
		"cookie" : "cookie"
	}
})

define(["jquery","cookie"],function(){
	function fn(){
		$(".sidebar_top a").click(function(){
        var speed=200;//滑动的速度
        $('body,html').animate({scrollTop:0},speed);
	   	     return false;
	 	});
	 	$(".side-qq").click(function(){/*点击QQ*/
	 		$(".qq_service").css("display","block");
	 	});
	 	$(".service_back").click(function(){
	 		$(this).parent().css("display","none");
	 	});
	 	
	 	$(".nav_right").mouseover(function(){/*移入购物车*/
	 		$(".nav_buycar").addClass("nav_buycaract");
	 		$(".nav_buyslit").css("display","block");
	 	});

	 	$(document).click(function(){/*购物车失焦事件*/
	 		$(".nav_buycar").removeClass("nav_buycaract");
	 		$(".nav_buyslit").css("display","none");
	 	});
	 	$(".nav_right").click(function(event){
	 		event.stopPropagation();
	 	});
	 	
	 	var user = getCookie("active_user");/*添加当前用户名字*/
	 	if(user){
	 		$(".top_login").html("您好" + user + "&nbsp;&nbsp; <a href='javascript:;' class='usequit'>退出！</a>");
			var $buycar = 0;/*添加当前用户购物车数量*/
			var $product = getCookie('users_' + user).split("-");
			if($product != ""){
				for(var j = 0;j < $product.length;j ++){
					var show = JSON.parse($product[j]);
					$buycar += parseInt(show.num);
				}	
			}
			$(".buycarnum").text($buycar);
	 	}else{
			$(".nav_buyslit").html("<div style='padding:30px 0;text-align:center;'>您的购物车是空的</div>")	
		}
	 	
	 	$(".top_login .usequit").click(function(){/*用户退出事件*/
	 		removeCookie("active_user");
	 		window.location.reload();
	 	});
	 	
	 	
	 	var $buycoo = getCookie('users_' + getCookie("active_user"));
		var $buypro = $buycoo.split("-");
		if($buypro != ""){
			$(".buyslit_ul li[reset='block']").remove();
			var $aggregate = 0;
			for(var j = 0;j < $buypro.length;j ++){
				var _this = $(".buyslit_ul li:first-child").clone(true).appendTo(".buyslit_ul").attr("reset","block").css("display","block");
				var show = JSON.parse($buypro[j]);
				_this.find(".buyslit_ul-img img").attr("src",show.img);
				_this.find(".buyslit_ul-title").text(show.id);
				_this.find(".buyslit_ul-price").text("￥" + show.price);
				_this.find(".buyslit_ul-num").text(show.num);
				$aggregate += show.price * show.num;
			}
			$(".buyslit_close .close_z span").text("￥" + $aggregate);
			
			/*可能因为模块的原因没有执行*/
			$("buyslit_ul-operate div a:first-child").click(function(){
				console.log("1231231523123123123");
				console.log($(this).parent().parent().siblings(".buyslit_ul-title").text());
				for(var j = 0;j < $buypro.length;j ++){
					var show = JSON.parse($buypro[j]);
					if($(this).parent().parent().siblings(".buyslit_ul-title").text() == show.id){
						var num = parseInt($(this).siblings(".buyslit_ul-num").text()) - 1;
						var $cookieValue = '{"id":"' + show.id + '","price":"' + show.price + '","num":"' + (num - 1) + '","img":"' + show.img + '"}';
						$(this).siblings(".buyslit_ul-num").text(num);
						var $cookieKey = 'users_' + getCookie("active_user");
						$buypro.splice(j,1);
						if($buypro.length == 0){
							setCookie($cookieKey , $cookieValue,7);
						}else{
							setCookie($cookieKey , $buypro.join("-") + "-" + $cookieValue,7);
						}
						break;
					}
				}
			});
			$("buyslit_ul-operate div a:last-child").click(function(){
				
			});
			$("buyslit_ul-remove").click(function(){
				
			});
		}
		
			
		
		
		
	}
	return {
		fn : fn
	}
})
